const fileInput = document.getElementById('fileInput');
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
const result = document.getElementById('result');
const calledStudentsDiv = document.getElementById('calledStudents');
// 获取倒计时元素
const countdownElement = document.getElementById('countdown');

let students = [];
let remainingStudents = [];
let intervalId;
let calledStudents = [];
let autoStopTimer;
let countdownTimer;

fileInput.addEventListener('change', function () {
  const file = this.files[0];
  if (file) {
    const reader = new FileReader();
    reader.onload = function (e) {
      const content = e.target.result;
      students = content.split('\n').filter(name => name.trim()!== '');
      remainingStudents = [...students];
      startButton.disabled = false;
      result.textContent = '学生名单已导入，点击开始点名';
      calledStudents = [];
      calledStudentsDiv.textContent = '已点名学生：';
    };
    reader.readAsText(file);
  }
});

startButton.addEventListener('click', function () {
  if (remainingStudents.length === 0) {
    remainingStudents = [...students];
    calledStudents = [];
    calledStudentsDiv.textContent = '已点名学生：';
  }
  startButton.disabled = true;
  stopButton.disabled = false;
  intervalId = setInterval(() => {
    const randomIndex = Math.floor(Math.random() * remainingStudents.length);
    result.textContent = remainingStudents[randomIndex];
  }, 100);

  // 设置 5 秒后自动停止点名的定时器
  autoStopTimer = setTimeout(() => {
    if (intervalId) {
      clearInterval(intervalId);
      clearInterval(countdownTimer);
      const selectedStudent = result.textContent;
      const index = remainingStudents.indexOf(selectedStudent);
      if (index!== -1) {
        remainingStudents.splice(index, 1);
        calledStudents.push(selectedStudent);
        calledStudentsDiv.textContent = `已点名学生：${calledStudents.join(', ')}`;
      }
      if (remainingStudents.length === 0) {
        result.textContent = '本轮点名结束，开始新的一轮点名';
        remainingStudents = [...students];
        calledStudents = [];
        calledStudentsDiv.textContent = '已点名学生：';
        startButton.disabled = false;
        stopButton.disabled = true;
      } else {
        startButton.disabled = false;
        stopButton.disabled = true;
        result.textContent = `已点到：${selectedStudent}，剩余 ${remainingStudents.length} 名学生未点`;
      }
      countdownElement.textContent = '';
    }
  }, 5000);

  // 开始 5 秒倒计时
  let timeLeft = 5;
  countdownElement.textContent = `倒计时: ${timeLeft} 秒`;
  countdownTimer = setInterval(() => {
    timeLeft--;
    if (timeLeft >= 0) {
      countdownElement.textContent = `倒计时: ${timeLeft} 秒`;
    } else {
      clearInterval(countdownTimer);
    }
  }, 1000);
});

stopButton.addEventListener('click', function () {
  clearInterval(intervalId);
  clearInterval(countdownTimer);
  // 清除自动停止点名的定时器
  clearTimeout(autoStopTimer);
  const selectedStudent = result.textContent;
  const index = remainingStudents.indexOf(selectedStudent);
  if (index!== -1) {
    remainingStudents.splice(index, 1);
    calledStudents.push(selectedStudent);
    calledStudentsDiv.textContent = `已点名学生：${calledStudents.join(', ')}`;
  }
  if (remainingStudents.length === 0) {
    result.textContent = '本轮点名结束，开始新的一轮点名';
    remainingStudents = [...students];
    calledStudents = [];
    calledStudentsDiv.textContent = '已点名学生：';
    startButton.disabled = false;
    stopButton.disabled = true;
  } else {
    startButton.disabled = false;
    stopButton.disabled = true;
    result.textContent = `已点到：${selectedStudent}，剩余 ${remainingStudents.length} 名学生未点`;
  }
  countdownElement.textContent = '';
});